Imports CasClientBase1
Imports ccapps.persistent.batchJobRequest
Imports System.Threading
Imports System.Timers.ElapsedEventArgs
Namespace batchjobs


    Public Class sms_update1
        Public errstr As String = ""

        Sub Tariff_Update2()

            '************* company kontrolleri yapiliyor ***************
            Dim sql As String
            Dim temp As DataTable
            Dim myRow As DataRow
            Dim s1 As New CasClientBase1.Class1
            Dim giris As String = s1.Login("10202", "lhssert01")
            Dim custcode As String
            Dim custid As String
            Dim coid As String
            Dim sw As System.IO.StreamWriter

            sw = System.IO.File.AppendText("c:\asp_job_log\oo2.txt")
            sw.WriteLine(giris)
            sw.Flush()
            sw.Close()

            Try
                If giris <> "1" Then
                    If giris.IndexOf("No listener available") > -1 Or giris.IndexOf("CAS was gone !") > -1 Then
                        s1.Send("[CANCEL()]")
                        s1.Logout()
                        giris = s1.Login("10202", "lhssert01")
                    Else
                        s1.Send("[CANCEL()]")
                        s1.Logout()
                        errstr += giris + vbNewLine
                        Exit Sub
                    End If
                End If
                If giris <> "1" Then
                    s1.Send("[CANCEL()]")
                    s1.Logout()
                    errstr += giris + vbNewLine
                    sw = System.IO.File.AppendText("c:\asp_job_log\oo2.txt")
                    sw.WriteLine(giris)
                    sw.Flush()
                    sw.Close()
                    Exit Sub
                End If
                sw = System.IO.File.AppendText("c:\asp_job_log\oo2.txt")
                sw.WriteLine(giris)
                sw.WriteLine(Now.TimeOfDay.ToString)
                sw.Flush()
                sw.Close()

                Dim i As Integer = 1

                For Each myRow In select_customers_for_sms2().Rows
                    custid = myRow(0)
                    coid = myRow(1)

                    s1.Send("[CANCEL()]")

                    Dim customer As New ccapps.model.cas.Customer
                    Dim dd1 As String = s1.Send("[CUSTOMER.EXISTS(""" & custid & """)]")

                    If dd1.IndexOf("ERROR") > 0 Or dd1.IndexOf("RC0000") < 0 Then
                        errstr += custid + "- " + dd1.Substring(0, dd1.IndexOf("]") + 1) + vbNewLine
                        s1.Send("[CANCEL()]")
                    Else

                        Dim dd2 As String = s1.Send("[CUSTOMER.READ(""" & custid & """)]")

                        If dd2.IndexOf("ERROR") > 0 Or dd2.IndexOf("RC0000") < 0 Then
                            errstr += custid + "- " + dd2.Substring(0, dd2.IndexOf("]") + 1) + vbNewLine
                            s1.Send("[CANCEL()]")
                        Else
                            customer.Parse(dd2)
                            customer.cs_rp_code = "14"
                            Dim a As String = s1.Send(customer.Write())

                            If a.IndexOf("ERROR") > 0 Or a.IndexOf("RC0000") < 0 Then
                                errstr += custid + "- " + a.Substring(0, a.IndexOf("]") + 1) + vbNewLine
                                s1.Send("[CANCEL()]")
                            End If

                            Dim b As String = s1.Send("[COMMIT_CUSTOMER(""1"")]")
                            If b.IndexOf("ERROR") > 0 Or b.IndexOf("RC0000") < 0 Then
                                errstr += custid + "- " + b.Substring(0, a.IndexOf("]") + 1) + vbNewLine
                                s1.Send("[CANCEL()]")
                            End If
                        End If

                    End If
                    s1.Send("[CANCEL()]")

                    ContractReadWrite(coid, s1, "14")

                Next



                sw = System.IO.File.AppendText("c:\asp_job_log\oo2.txt")
                sw.WriteLine(errstr)
                sw.WriteLine(giris + Now.TimeOfDay.ToString)
                sw.Flush()
                sw.Close()



            Catch ex As Exception
                Dim a2 As String = s1.Send("[CANCEL()]")
                Dim sonuc As String = s1.Logout()
                errstr += ex.ToString + vbNewLine
                sw = System.IO.File.AppendText("c:\asp_job_log\oo2.txt")
                sw.WriteLine(errstr)
                sw.Flush()
                sw.Close()
            End Try
        End Sub

        Function isError(ByVal inputResult As String, ByVal param1 As String, ByVal param2 As String, ByVal s1 As CasClientBase1.Class1) As Integer

            If inputResult.IndexOf("ERROR") > 0 Or inputResult.IndexOf("RC0000") < 0 Then
                errstr += param1 + "     " + inputResult.Substring(0, inputResult.IndexOf("]") + 1) + vbNewLine

                If inputResult.IndexOf("RC6014") > 0 Then
                    errstr += "Gece isleme alinacak" + vbNewLine
                    s1.Send("[CANCEL()]")
                End If

                s1.Send("[CANCEL()]")
                Return 1
            End If

            Return 0

        End Function

        Function ContractReadWrite(ByVal coid As String, ByVal s1 As CasClientBase1.Class1, ByVal tmcode As String)

            Dim contract As New ccapps.model.cas.Contract

            Dim dd1 As String = s1.Send("[CONTRACT.EXISTS("""",""" & coid & """)]")
            If isError(dd1, "coid:" + coid, "", s1) = 1 Then Return Nothing

            Dim dd2 As String = s1.Send("[CONTRACT.READ(""" & coid & """)]")
            If isError(dd2, "coid:" + coid, "", s1) = 1 Then Return Nothing

            contract.Parse(dd2)
            Dim cnt As Integer

            contract.co_rp_code = tmcode

            Dim a As String = s1.Send(contract.Write())
            If isError(a, "coid:" + coid, "", s1) = 1 Then Return Nothing


            Dim b As String = s1.Send("[COMMIT_CONTRACT(""1"","""","""","""","""")]")
            If isError(b, "coid:" + coid, "", s1) = 1 Then Return Nothing

            s1.Send("[CANCEL()]")

        End Function



    End Class
End Namespace